//
// Created by A on 2018/7/4.
//

#include "MemCheckerC.h"
#include "MemRecoder.h"


void memChecker_init() {
}

void memChecker_printAllMemUsage() {
    printAllRecord();
}

void memChecker_printLeakMem() {
    printLeakRecord();
}

void memChecker_dumpAllMemUsage(char *filePath) {
    dumpAllRecord(filePath);
}

void memChecker_dumpLeakMem(char *filePath) {
    dumpLeakRecord(filePath);
}

void memChecker_release() {
    releaseRecord();
}


void *inner_malloc(unsigned int uSize, const char *filePath, const char *pszFunc, unsigned int uLine) {
    void *pPtr = malloc(uSize);

    char *des = (char *) malloc(256);
    memset(des, 0, 256);
    sprintf(des, "%s:%s(%d)[%s %p : %d bytes long.]", filePath, pszFunc, uLine, "malloc", pPtr , uSize);
    int desLen = (int) strlen(des);
    recordMalloc(des, desLen, (long) pPtr);
    free(des);

    return pPtr;   //调用标准C库的malloc.

}

void inner_free(void *pPtr, const char *filePath, const char *pszFunc, unsigned int uLine) {

    char *des = (char *) malloc(256);
    memset(des, 0, 256);
    sprintf(des, "%s:%s(%d)[%s %p]", filePath, pszFunc, uLine, "free", pPtr);
    int desLen = (int) strlen(des);
    recordFree(des, desLen, (long) pPtr);
    free(des);

    //
    free(pPtr);
    pPtr = NULL;
}
